﻿using System;
using System.Linq;
using System.ComponentModel;
using System.Windows.Data;
using System.Collections.ObjectModel;

using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;

using CatalogModule.Models;
using CatalogModule.Services;
using ServiceAgent;
using ServiceAgent.CatalogueService;
using Infrastructure;
using Infrastructure.Helpers;
using Infrastructure.Events;
using System.Windows.Input;
using System.Windows;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using CatalogModule.Views;
using CatalogModule.Events;

namespace CatalogModule.ViewModels
{
    /// <summary>
    /// ViewModel for DoanTheView.
    /// </summary>
    public class DoanTheViewModel : NotificationObject, INavigationAware
    {
        #region Fields
        private readonly IEventAggregator eventAggregator;
        private readonly IUnityContainer container;
        private readonly IRegionManager regionManager;

        ObservableCollection<HoatDongDoanTheDTO> list;
        HoatDongDoanTheDTO currentItem;

        public InteractionRequest<Notification> AddNewRequest { get; private set; }
        #endregion

        #region Constructor
        /// <summary>
        /// Default constructor.
        /// </summary>
        public DoanTheViewModel(IEventAggregator eventAggregator, IRegionManager regionManager, IUnityContainer container)
        {
            this.eventAggregator = eventAggregator;
            this.regionManager = regionManager;
            this.container = container;

            this.container.RegisterType<Object, AddDoanTheView>("AddDoanTheView");
            this.eventAggregator.GetEvent<AddDoanTheEvent>().Subscribe(this.RaiseListChanged);
            this.Initialize();
            this.GetHoatDongDoanTheList();
        }
        #endregion

        #region Administrative Properties
        public ObservableCollection<HoatDongDoanTheDTO> List
        {
            get
            {
                return list;
            }
            set
            {
                list = value;
                RaisePropertyChanged(() => this.List);
            }
        }

        public ServiceAgent.CatalogueService.HoatDongDoanTheDTO CurrentItem
        {
            get { return currentItem; }
            set
            {
                currentItem = value;
                RaisePropertyChanged(() => this.CurrentItem);
                this.DeleteCommand.RaiseCanExecuteChanged();
            }
        }
        #endregion

        #region Command Properties
        public ICommand AddNewCommand { get; private set; }
        public ICommand SaveCommand { get; private set; }
        public DelegateCommand DeleteCommand { get; private set; }
        #endregion

        #region Private Methods
        private void Initialize()
        {
            this.AddNewCommand = new DelegateCommand(this.OnRaiseAddNew);
            this.SaveCommand = new DelegateCommand(this.OnRaiseSave);
            this.DeleteCommand = new DelegateCommand(this.OnRaiseDelete, this.CanRaiseDelete);
            this.AddNewRequest = new InteractionRequest<Notification>();
        }

        void GetHoatDongDoanTheList()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    List = proxy.GetHoatDongDoanTheList();
                }
            }
            catch (Exception)
            {

            }
        }

        private void OnRaiseAddNew()
        {
            this.AddNewRequest.Raise(
               new Notification { Title = "Thêm mới" },
               dummy => { this.GetHoatDongDoanTheList(); });
        }

        private void OnRaiseSave()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    proxy.SaveHoatDongDoanTheList(List);
                    RaisePropertyChanged(() => this.List);
                    MessageBox.Show("Lưu thành công");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        private void OnRaiseDelete()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    proxy.RemoveHoatDongDoanThe(CurrentItem);
                    this.List.Remove(CurrentItem); 
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        private bool CanRaiseDelete()
        {
            return CurrentItem != null;
        }

        public void RaiseListChanged(HoatDongDoanTheDTO obj)
        {
            if (obj != null)
            {
                this.List.Add(obj);
            }
        }
        #endregion

        #region INavigationAware Members
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            // Called to see if this view can handle the navigation request.
            // If it can, this view is activated.
            return false;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

            // Called when this view is deactivated as a result of navigation to another view.
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            //// Called to initialize this view during navigation.

            //// Retrieve any required paramaters from the navigation Uri.
            //string id = navigationContext.Parameters["ID"];
            //if (!string.IsNullOrEmpty(id))
            //{
            //    DataItem item = _dataService.GetModel().FirstOrDefault(dataItem => dataItem.Id == id);
            //}
        }
        #endregion
    }
}
